package com.example.onlineeducation.mapper

import com.example.onlineeducation.data.entity.UserProStudyChartEntity
import com.example.onlineeducation.data.json.UserProStudyChartJson
import com.example.onlineeducation.utils.YcTimeUtils
import com.github.mikephil.charting.data.PieEntry
import com.yc.jetpacklib.extension.ycToNoEmpty
import com.yc.jetpacklib.mapper.IMapper
import com.yc.jetpacklib.widget.chart.YcEntry
import java.math.BigDecimal

class UserProStudyChartMapper : IMapper<UserProStudyChartJson?, UserProStudyChartEntity> {
    override fun map(input: UserProStudyChartJson?): UserProStudyChartEntity {
        return if (input != null) {
            val studyWeekEntryY: MutableList<YcEntry> = mutableListOf()
            val studyWeekEntryX: MutableList<String> = mutableListOf()
            var studyWeekEntryYMax = 0f
            for ((i, item) in input.dayDashboardList!!.withIndex()) {
                val time = BigDecimal((item.totalTimes ?: 0) / 60.0).setScale(2, BigDecimal.ROUND_HALF_UP).toFloat()
                if (time > studyWeekEntryYMax) {
                    studyWeekEntryYMax = time
                }
                studyWeekEntryY.add(YcEntry(i, time))
                studyWeekEntryX.add(YcTimeUtils.stringToString(item.dateStr, "无"))
            }

            val studyWeekType: MutableList<PieEntry> = mutableListOf()
            var studyTypeOther = 0L//tag为3或tag为空都属于"其他"类型
            for ((i, item) in input.tagDashboardList!!.withIndex()) {
                if (item.tag == 3L || item.tag == null) {
                    studyTypeOther += item.times ?: 0
                } else {
                    val time = item.times ?: 0
                    studyWeekType.add(PieEntry(time.toFloat(), "${item.tagName.ycToNoEmpty()}:${YcTimeUtils.secondToString(time)}"))
                }
            }
            if (studyTypeOther > 0) {
                studyWeekType.add(PieEntry(studyTypeOther.toFloat(), "其他:${YcTimeUtils.secondToString(studyTypeOther)}"))
            }
            UserProStudyChartEntity(studyWeekEntryX, studyWeekEntryY, studyWeekEntryYMax, studyWeekType)
        } else {
            UserProStudyChartEntity.empty()
        }
    }
}

